package com.ibm.risk.irmp.base.file.service.impl;

import com.ibm.risk.irmp.base.file.service.FileStoreService;
import com.ibm.risk.irmp.common.utils.ObsBucketUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.util.Date;

@Slf4j
public class HuaweiObsFileStoreServiceImpl implements FileStoreService {

    @Value("${app.irmp.base.filestore.obs.bucket-name}")
    private String bucketName;
    @Autowired
    ObsBucketUtils obsFileUtils;


    @Override
    public String upload(File localFile, String path, String fileName) throws Exception {
        String dts = DateFormatUtils.format(new Date(), "yyyyMMddHHmmssSSS");
        String objectKey = path + "/" + dts + "_" + fileName;
        objectKey = obsFileUtils.upload(localFile, objectKey, bucketName);
        return objectKey;
    }

    @Override
    public void download(String fileUrl, File localFile) throws Exception {
        obsFileUtils.download(fileUrl, localFile, bucketName);
    }

    @Override
    public void delete(String remoteUri) throws Exception {
        obsFileUtils.delete(remoteUri, bucketName);
    }

    @Override
    public String getFileStoreSys() {
        return "huawei-obs";
    }
}
